package com.sojson.project.sys.dict.controller;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sojson.config.exception.TransErrorCode;
import com.sojson.project.query.entity.bo.Select;
import com.sojson.project.sys.dict.controller.param.verify.DictTypeParamVerify;
import com.sojson.project.sys.dict.entity.dto.DictTypeDto;
import com.sojson.project.sys.dict.entity.dto.DictTypePageDto;
import com.sojson.project.sys.dict.entity.po.DictType;
import com.sojson.project.sys.dict.entity.vo.DictTypeVo;
import com.sojson.project.sys.dict.service.DictTypeService;
import com.sojson.result.Result;
import com.sojson.util.DictUtil;
import com.sojson.util.poi.ExcelUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 字典类型表控制器
 * 
 * @author liu
 * @date 2022-09-07
 */
@Api(tags = "字典类型表管理")
@RestController
@RequestMapping("/sys/dict/type")
public class DictTypeController {

    @Autowired
    private DictTypeService dictTypeService;

    /**
     * 单条插入
     * 
     * @param dto
     * @return
     */
    @PostMapping("/save")
    @PreAuthorize("@ss.hasPermi('/sys/dict/type/save')")
    @ApiOperation("给字典类型表添加一条数据")
    public Result<Boolean> save(@RequestBody @Validated DictType dto) {
        // 校验参数
        Result<Boolean> paramResult = DictTypeParamVerify.save(dto);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        // 去重
        LambdaQueryWrapper<DictType> query = Wrappers.lambdaQuery(DictType.class);
        query.eq(DictType::getCode, dto.getCode());
        int count = dictTypeService.count(query);
        if (count > 0) {
            return Result.fail("代码已存在!");
        }

        dto.saveInit();
        return Result.success(dictTypeService.save(dto));
    }

    /**
     * 通过id更新
     * 
     * @param dto
     * @return
     */
    @PutMapping("/updateById")
    @PreAuthorize("@ss.hasPermi('/sys/dict/type/updateById')")
    @ApiOperation("通过id更新字典类型表数据")
    public Result<Boolean> updateById(@RequestBody DictType dto) {
        // 校验参数
        Result<Boolean> paramResult = DictTypeParamVerify.updateById(dto);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        dto.updateInit();
        return Result.success(dictTypeService.updateById(dto));
    }

    /**
     * 分页查询
     * 
     * @param dto
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermi('/sys/dict/type/page')")
    @ApiOperation(value = "分页查询字典类型表数据", response = DictTypeVo.class)
    public Result<List<DictTypeVo>> page(DictTypePageDto dto) {
        return Result.success(dictTypeService.page(dto));
    }

    /**
     * 导出数据
     * 
     * @param dto
     * @return
     * @throws Exception 
     */
    @GetMapping("/export")
    @PreAuthorize("@ss.hasPermi('/sys/dict/type/export')")
    @ApiOperation("导出字典类型表数据")
    public void export(DictTypeDto dto) throws Exception {
        List<DictTypeVo> list = dictTypeService.list(dto);
        ExcelUtil<DictTypeVo> excelUtil = new ExcelUtil<>(DictTypeVo.class);
        excelUtil.exportExcelOutFile(list, "字典类型");
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    @PreAuthorize("@ss.hasPermi('/sys/dict/type/info/{id}')")
    @ApiOperation(value = "通过id查询字典类型表数据", response = DictTypeVo.class)
    public Result<DictTypeVo> info(@PathVariable String id) {
        if (id == null) {
            return Result.success();
        }

        return Result.success(dictTypeService.findById(id));
    }

    /**
     * 通过id列表删除
     * 
     * @param dto
     * @return
     * @throws IOException 
     */
    @DeleteMapping("/remove")
    @PreAuthorize("@ss.hasPermi('/sys/dict/type/remove')")
    @ApiOperation("通过id列表删除字典类型表数据")
    public Result<Boolean> remove(@RequestBody DictTypeDto dto) throws IOException {
        List<String> ids = dto.getIds();
        if (ids == null || ids.size() == 0) {
            return Result.success();
        }

        return Result.success(dictTypeService.removeByIds(ids));
    }

    /**
     * 清理缓存
     * 
     * @return
     * @throws IOException 
     */
    @DeleteMapping("/clearCache")
    @PreAuthorize("@ss.hasPermi('/sys/dict/type/clear')")
    @ApiOperation("清理字典类型表缓存")
    public Result<Boolean> clearCache() throws IOException {
        DictUtil.reload();
        return Result.success();
    }

    /**
     * 查询下拉列表
     * 
     * @param dto
     * @return
     */
    @GetMapping("/findSelect")
    @PreAuthorize("@ss.hasPermi('/sys/dict/type/findSelect')")
    @ApiOperation(value = "查询字典类型下拉列表", response = Select.class)
    public Result<List<Select>> findSelect(DictTypeDto dto) {
        List<Select> findByDto = dictTypeService.findSelect(dto);
        return Result.success(findByDto);
    }

}